/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package x10.interfaces.cm15a.transmit;

import java.util.logging.Logger;

import x10.enums.Media;
import x10.event.X10Command;
import x10.event.X10Event;
import x10.event.X10InterfaceRequest;
import x10.event.X10Remote;
import x10.event.X10SecurityCommand;
import x10.event.X10SecuritySensor;
import x10.queues.X10EventQueue;
import x10.queues.X10TransmitQueue;
import x10.utilities.ChunkOfBytes;

/**
 *
 * @author Denny
 */
public class CM15ATransmitFactory {
    Logger logger = Logger.getLogger(this.getClass().getName());
    X10TransmitQueue transmitQueue = new X10TransmitQueue();
    X10EventQueue deviceQueue = new X10EventQueue();
    X10DeviceTransmitter transmitter;
    boolean autoProcessEnabled = true;

    public CM15ATransmitFactory(){
    }

    public void enqueue(X10Event device){

	deviceQueue.add(device);

	if(autoProcessEnabled){
            process();
        }
    }

    public boolean isAutoProcessEnabled() {
        return autoProcessEnabled;
    }

    //Set autoProcessEnabled = false, then enqueue multiple devices
    //then call process() and finally set autoProcessEnabled = true
    //For use when executing a macro, to queue up a bunch of devices
    public void setAutoProcessEnabled(boolean autoProcessEnabled) {
        this.autoProcessEnabled = autoProcessEnabled;
    }

    public boolean hasBytesToTransmit(){
        return transmitQueue.hasNext();
    }

    public ChunkOfBytes getBytes(){
        return transmitQueue.pop();
    }

    public void process(){

        //Determine X10Device class, spawn transmitter and transmit

        //if() statements arranged in priority, giving security classes
        //first check, then commands, remotes and finally interfaces

        while(deviceQueue.hasNext()){
            X10Event device = deviceQueue.pop();

            //X10SecurityCommand class
            if (device.getClass() == X10SecurityCommand.class){

            //X10SecuritySensor class
            } else if (device.getClass() == X10SecuritySensor.class){

            //X10Command class
            } else if (device.getClass() == X10Command.class) {

                //Determine the media and transmit the device over the correct media
                X10Command command = (X10Command)device;
                Media media = command.getMedia();

                //X10Command - Powerline
                if(media == Media.POWERLINE){
                    transmitter = new PLCommandFactory(transmitQueue);
                    transmitter.transmit(device);

                //X10Command - Radio Frequency
                } else if (media == Media.RADIO_FREQUENCY){
                    transmitter = new RFCommandFactory(transmitQueue);
                    transmitter.transmit(device);
                }

            //X10Remote class
            } else if (device.getClass() == X10Remote.class) {
                transmitter = new RFRemoteFactory(transmitQueue);
                transmitter.transmit(device);

            //X10 Interface Request class
            } else if (device.getClass() == X10InterfaceRequest.class){
                transmitter = new IFRequestFactory(transmitQueue);
                transmitter.transmit(device);

            } else {
        	logger.warning("Unable to transmit -- unknown X10Device - ID: " + device.getAddress());
            }
        }
    }

}
